﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

namespace zjbks
{
    public partial class KaoShengBaoMing : System.Web.UI.Page
    {
        public static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["zjbksConnectionString"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            id.Text=Session["id"].ToString();
            if(int.Parse(Session["id"].ToString().Substring(14,3))%2==0)
            {
                gender.Items[1].Selected=true;
            }
            else
            {
                gender.Items[0].Selected = true;
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Model.StudentInfoEntity studentInfo = new Model.StudentInfoEntity();
            studentInfo.ID = id.Text;
            studentInfo.Name = name.Text;
            studentInfo.Nation = nationinfo.SelectedValue;
            studentInfo.Phone = phone.Text;
            studentInfo.Sex = gender.SelectedValue;
            studentInfo.Subject = subjectinfo.SelectedValue;
            studentInfo.email = email.Text;
            studentInfo.Collage = collageinfo.SelectedValue;
            Test test = new Test();
            //if (test.phone(studentInfo.Phone))
            //{
            //    ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('手机号有误！');</script>");
            //    return;
            //}
            string sql = "select name from province where code='" + DropDownList1.Text + "'";
            SqlConnection conn = new SqlConnection(ConnectionString);
            SqlCommand cmdpro = new SqlCommand(sql);
            cmdpro.Connection = conn;
            conn.Open();
            string pro = cmdpro.ExecuteScalar().ToString();
            conn.Close();
            string sql1 = "select name from city where code='"+ DropDownList2.Text + "'";
            SqlCommand cmdcity = new SqlCommand(sql1);
            cmdcity.Connection = conn;
            conn.Open();
            string city = cmdcity.ExecuteScalar().ToString();
            conn.Close();
            string sql2 = "select * from area where code='" + DropDownList3.Text + "'";
            SqlCommand cmderea = new SqlCommand(sql2);
            cmderea.Connection = conn;
            conn.Open();
            string erea = cmdcity.ExecuteScalar().ToString();
            conn.Close();
            studentInfo.Address =pro + city +erea + other.Text;
            studentInfo.BMtime = DateTime.Now.Date;
            BLL.BStudentInfo bStudent = new BLL.BStudentInfo();
            Model.StudentInfoEntity studentInfo2 = bStudent.GetStudentInfo(studentInfo.ID);
            if(studentInfo2!=null)
            {
                ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('不能重复报名！');</script>");
            }
            else
            {
                if (bStudent.AddStudentInfo(studentInfo) > 0)
                {
                    ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('报名成功！');</script>");
                }
                else
                {
                    ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('报名失败！');</script>");
                }
            }

        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (SqlDataSource2.SelectParameters.Count > 1)
                SqlDataSource2.SelectParameters.RemoveAt(1);
            SqlDataSource2.SelectParameters.Add("@provincecode", DropDownList1.SelectedValue);
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (SqlDataSource3.SelectParameters.Count > 1)
                SqlDataSource3.SelectParameters.RemoveAt(1);
            SqlDataSource3.SelectParameters.Add("@citycode", DropDownList2.SelectedValue);
        }

        //protected string getstring(string tablenme,string code)
        //{
        //    SqlConnection conn = new SqlConnection();
        //}

        protected void Button3_Click(object sender, EventArgs e)
        {

        }
    }
}